//
//  SHPAlbumCollectionView.swift
//  weibo
//
//  Created by songhuipan on 16/4/24.
//  Copyright © 2016年 songhuipan. All rights reserved.
//

import UIKit

let collectionViewId = "collectionViewId"

class SHPAlbumCollectionView: UICollectionView {
    
    var images:[UIImage] = [UIImage]()
    // MARK:点击添加图片cell执行的闭包
    var closeure:(()->())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK:添加图片的方法
    func addImage(image:UIImage){
        images.insert(image, atIndex: 0)
        if images.count > 9{
            images.removeLast()
        }
        if images.count > 0{
            self.hidden = false
        }
        reloadData()
    }
    // MARK:设置视图UI
    func setupUI(){
        self.hidden = true
        dataSource = self
        delegate = self
        backgroundColor = UIColor.whiteColor()
        registerClass(SHPAlbumCell.self, forCellWithReuseIdentifier: collectionViewId)
        let flowLayout = self.collectionViewLayout as! UICollectionViewFlowLayout
        let margin :CGFloat = 5
        let itemWH = (kScreenWidth - 20 - margin*2)/3
        flowLayout.itemSize = CGSizeMake(itemWH, itemWH)
        flowLayout.minimumInteritemSpacing = margin
        flowLayout.minimumLineSpacing = margin
        
    }
}
// MARK:UICollectionViewDataSource
extension SHPAlbumCollectionView:UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = images.count
        if count == 0 || count == 9{
            return count
        }else {
            return count + 1
        }
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewId, forIndexPath: indexPath) as! SHPAlbumCell
        
        if indexPath.item == images.count{
            cell.image = nil
        }else {
            cell.image = images[indexPath.item]
        }
        cell.collectionViewCloseure = {[weak self] in
            self?.images.removeAtIndex(indexPath.item)
            if self?.images.count <= 0{
                self?.hidden = true
            }
            self?.reloadData()
        }
        return cell
    }
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        if indexPath.item == images.count {
            closeure?()
        }
    }
}

class SHPAlbumCell: UICollectionViewCell {
    ///  点击删除按钮执行的闭包
    var collectionViewCloseure:(()->())?
    
    var image:UIImage?{
        didSet{
            if image == nil {
                backImageView.image = UIImage(named: "compose_pic_add")
                backImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                deleteButton.hidden = true
            }else {
                backImageView.highlightedImage = nil
                backImageView.image = image
                deleteButton.hidden = false
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK:设置视图UI
    func setupUI(){
        addSubview(backImageView)
        addSubview(deleteButton)
        backImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(CGSizeZero)
        }
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.top.trailing.equalTo(contentView)
        }
        
    }
    // 懒加载背景图片视图
    private lazy var backImageView:UIImageView = UIImageView()
    // MARK:懒加载删除图片按钮
    private lazy var deleteButton:UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.sizeToFit()
        return btn
    }()
    // MARK:删除按钮的点击监听事件
    @objc private func deleteButtonClick(){
        collectionViewCloseure?()
    }
}

































